package com.lj.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.lj.vod.properties.AliyunVodProperties;
import com.lj.vod.service.VodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @author luojing
 * @version 1.0
 * @date 2021/5/18 11:15
 */
@Service
@Slf4j
public class VodServiceImpl implements VodService {

    @Autowired
    private AliyunVodProperties aliyunVodProperties;

    @Override
    public Map<String, Object> uploadVideo(MultipartFile file){
        InputStream in = null;
        UploadStreamResponse response = null;

        try {
            in = file.getInputStream();
            UploadStreamRequest request = new UploadStreamRequest(aliyunVodProperties.getKeyid(), aliyunVodProperties.getKeysecret(), file.getOriginalFilename(), file.getOriginalFilename(), in);
            /* 关闭默认上传进度回调 */
            request.setPrintProgress(false);
            UploadVideoImpl uploader = new UploadVideoImpl();
            response = uploader.uploadStream(request);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("IO异常");
        }finally {
            try {
                assert in != null;
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
                log.error("IO异常");
            }
        }

        Map<String, Object> map = new HashMap<>();
        map.put("RequestId", response.getRequestId());
        map.put("videoOriginalName",file.getOriginalFilename());
        if (response.isSuccess()) {
            map.put("VideoId", response.getVideoId());
        } else {
            /* 如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因 */
            map.put("VideoId", response.getVideoId());
            map.put("ErrorCode", response.getCode());
            map.put("ErrorMessage", response.getMessage());
        }
        return map;
    }

    private DefaultAcsClient initVodClient() {
        String regionId = "cn-shanghai";  // 点播服务接入区域
        DefaultProfile profile = DefaultProfile.getProfile(regionId, aliyunVodProperties.getKeyid(), aliyunVodProperties.getKeysecret());
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    @Override
    public Boolean deleteById(String id) {
        DefaultAcsClient client = initVodClient();
        DeleteVideoResponse response = null;
        DeleteVideoRequest request = new DeleteVideoRequest();
        //支持传入多个视频ID，多个用逗号分隔
        request.setVideoIds(id);
        try {
            response = client.getAcsResponse(request);
        } catch (Exception e) {
            log.error("ErrorMessage = " + e.getLocalizedMessage());
            return false;
        }
        log.info("RequestId = " + response.getRequestId());
        return true;
    }
}
